#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
const int N = 1100;
int f[N];
int T[N];
int a[N];
int t, m;
int main()
{
    cin >> t >> m;
    for (int i = 1; i <= m; i++)
    {
        cin >> T[i] >> a[i];
    }
    for (int i = 1; i <= m; i++)
    {
        for (int j = t; j >= T[i]; j--)
        {
            f[j] = max(f[j], f[j - T[i]] + a[i]);
        }
    }
    cout << f[t] << endl;
    return 0;
}